﻿using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using Koala.Pro.Common;
using Koala.Pro.Common.Extension;
using Koala.Pro.Core.Models;
using Koala.Pro.Helpers;
using Koala.Pro.ViewModels;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Wpf.Ui.Controls;

namespace Koala.Pro.Views.Pages
{
    public partial class UserSelectPage : Window, INavigableView<UserListViewModel>
    {
        public UserListViewModel ViewModel { get; }

        public UserSelectPage(UserListViewModel viewModel)
        {
            ViewModel = viewModel;
            base.DataContext = this;
            InitializeComponent();
            ViewModel.GetTreeCommand.ExecuteAsync(null);
            ViewModel.GetTreeRoleCommand.ExecuteAsync(null);
        }

        [RelayCommand]
        private void Search()
        {
            ViewModel.PageDto.Dept_ID = null;
            ViewModel.PageDto.Role_ID = null;
            ViewModel.SearchCommand.ExecuteAsync(null);
        }

        [RelayCommand]
        private void AddUsers()
        {
            var selectedItems = dataGrid.SelectedItems;
            if (selectedItems != null && selectedItems.Count > 0)
            {
                base.DialogResult = true;
            }
            else
            {
                MessageDxUtil.ShowTips("请选择至少一条记录");
            }
        }

        [RelayCommand]
        private void Back()
        {
            Close();
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!(sender is System.Windows.Controls.DataGrid { SelectedItems: var selectedItems }))
            {
                return;
            }
            if (selectedItems != null)
            {
                List<UserInfo> list = new List<UserInfo>();
                foreach (UserInfo item in selectedItems)
                {
                    list.Add(item);
                }
                ViewModel.SelectedItems = list;
            }
            else
            {
                ViewModel.SelectedItems = null;
            }
        }

        private string GetRowData(DataRow row, string columnName)
        {
            string result = "";
            if (row.Table.Columns.Contains(columnName))
            {
                result = row[columnName].ToString();
            }
            return result;
        }

        private async void deptTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue is RoleAndDeptTreeNode roleAndDeptTreeNode)
            {
                ViewModel.DeptTreeModel.SelectNode = roleAndDeptTreeNode;
                ViewModel.PageDto.Company_ID = roleAndDeptTreeNode.CompanyId;
                ViewModel.PageDto.Dept_ID = roleAndDeptTreeNode.DeptId;
                ViewModel.PageDto.Role_ID = null;
                await ViewModel.GetData();
            }
        }

        private void SearchBar_OnSearchStarted(object sender, FunctionEventArgs<string> e)
        {
            ViewModel.DeptTreeModel.SearchKey = e.Info;
            if (e.Info.IsNullOrEmpty())
            {
                ViewModel.DeptTreeModel.FilteredTreeItems = ViewModel.DeptTreeModel.TreeItems;
                return;
            }
            ViewModel.DeptTreeModel.FilteredTreeItems = ViewModel.DeptTreeModel.TreeItems.FindRoleNodes((RoleAndDeptTreeNode s) => s.Name.Contains(e.Info, StringComparison.OrdinalIgnoreCase));
        }

        private async void roleTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue is RoleAndDeptTreeNode roleAndDeptTreeNode)
            {
                ViewModel.RoleTreeModel.SelectNode = roleAndDeptTreeNode;
                ViewModel.PageDto.Company_ID = roleAndDeptTreeNode.CompanyId;
                ViewModel.PageDto.Dept_ID = roleAndDeptTreeNode.DeptId;
                ViewModel.PageDto.Role_ID = roleAndDeptTreeNode.RoleId;
                await ViewModel.GetData();
            }
        }

        private void SearchBarRole_OnSearchStarted(object sender, FunctionEventArgs<string> e)
        {
            ViewModel.RoleTreeModel.SearchKey = e.Info;
            if (e.Info.IsNullOrEmpty())
            {
                ViewModel.RoleTreeModel.FilteredTreeItems = ViewModel.RoleTreeModel.TreeItems;
                return;
            }
            ViewModel.RoleTreeModel.FilteredTreeItems = ViewModel.RoleTreeModel.TreeItems.FindRoleNodes((RoleAndDeptTreeNode s) => s.Name.Contains(e.Info, StringComparison.OrdinalIgnoreCase));
        }
    }
}
